use log::{error, info};

use log::{Level, Metadata, Record};
use log::{LevelFilter, SetLoggerError};

struct SimpleLogger;

impl log::Log for SimpleLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= Level::Info
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            println!("{} - {}", record.level(), record.args());
        }
    }

    fn flush(&self) {}
}

static LOGGER: SimpleLogger = SimpleLogger;

pub fn init() -> Result<(), SetLoggerError> {
    log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Info))
}

// logging

pub fn ownership() {
    let mut x = 5;
    let y = x;
    x = 100;
    println!("{},{}", x, y);

    let mut s1 = String::from("hello");
    let s2 = s1;
    s1 = String::from("hello333");
    println!("{}", s2);
    println!("{}, world!", s1);

    let res = init();
    error!("Executing s: {}", res.is_ok());

    let s = "hello ownership"; // 从此处起，s 是有效的
    println!("{}", s);

    error!("Executing s: {}", s);
    info!("Executing s: {}", s);

    // 使用 s
}
